﻿using System;
using System.Diagnostics;
using System.Configuration;

namespace TQB.Page.Config
{
    public class HandlerMapSection : ConfigurationSection
    {
        /*
            <configSections>
                <section name="handlerMapping" type="HandlerMapSection"/>
            </configSections>
            <handlerMapping>
                <map methodName="getCustomerList" typeName="CustomerListHandler"/>
                <map methodName="topCustomers" typeName="TopCustomerHandler"/>
            </handlerMapping>
        */
        public const string SectionName = "handlerMapping";

        private static readonly ConfigurationProperty _maps = new ConfigurationProperty(string.Empty, typeof(HandlerMapCollection), null, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection);
        private static readonly ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();

        public HandlerMapCollection Maps
        {
            [DebuggerStepThrough()]
            get
            {
                return (HandlerMapCollection)base[_maps];
            }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            [DebuggerStepThrough()]
            get
            {
                return _properties;
            }
        }

        static HandlerMapSection()
        {
            _properties.Add(_maps);
        }
    }

    public class HandlerMapCollection : ConfigurationElementCollection
    {
        private static readonly ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();

        public HandlerMap this[int index]
        {
            [DebuggerStepThrough()]
            get
            {
                return (HandlerMap)base.BaseGet(index);
            }
            [DebuggerStepThrough()]
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }

                base.BaseAdd(index, value);
            }
        }

        public new HandlerMap this[string name]
        {
            [DebuggerStepThrough()]
            get
            {
                return (HandlerMap)base.BaseGet(name);
            }
        }

        protected override string ElementName
        {
            [DebuggerStepThrough()]
            get
            {
                return "map";
            }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            [DebuggerStepThrough()]
            get
            {
                return _properties;
            }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            [DebuggerStepThrough()]
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        public void Add(HandlerMap item)
        {
            base.BaseAdd(item);
        }

        public void Remove(HandlerMap item)
        {
            base.BaseRemove(item);
        }

        public void RemoveAt(int index)
        {
            base.BaseRemoveAt(index);
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new HandlerMap();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as HandlerMap).MethodName;
        }
    }

    public class HandlerMap : ConfigurationElement
    {
        private static readonly ConfigurationProperty _method = new ConfigurationProperty("methodName", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);
        private static readonly ConfigurationProperty _type = new ConfigurationProperty("typeName", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();

        [ConfigurationProperty("methodName", IsRequired = true)]
        public string MethodName
        {
            [DebuggerStepThrough()]
            get
            {
                return (string)base[_method];
            }
        }

        [ConfigurationProperty("typeName", IsRequired = true)]
        public string TypeName
        {
            [DebuggerStepThrough()]
            get
            {
                return (string)base[_type];
            }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            [DebuggerStepThrough()]
            get
            {
                return _properties;
            }
        }

        static HandlerMap()
        {
            _properties.Add(_method);
            _properties.Add(_type);
        }
    }
}
